<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="患者名字" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入患者名字" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="患者年龄" prop="age">
        <el-input v-model="queryParams.age" placeholder="请输入患者年龄" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="患者身份证" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入患者身份证" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="患者性别
" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择患者性别
" clearable size="small">
          <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['outpatient:archives:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['outpatient:archives:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['outpatient:archives:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['outpatient:archives:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="archivesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="患者id" align="center" prop="id" />
      <el-table-column label="患者名字" align="center" prop="name" />
      <el-table-column label="患者年龄" align="center" prop="age" />
      <el-table-column label="患者身份证" align="center" prop="code" />
      <el-table-column label="患者性别" align="center" prop="sex">
        <template slot-scope="scope">
          <el-button v-if="scope.row.sex==0">男</el-button>
          <el-button v-if="scope.row.sex==1">女</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['outpatient:archives:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['outpatient:archives:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改个人档案对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
    			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
    				<el-form-item label="患者名字" prop="name">
    					<el-input v-model="form.name" placeholder="请输入患者名字" />
    				</el-form-item>
    				<el-form-item label="患者年龄" prop="age">
    					<el-input v-model="form.age" placeholder="请输入患者年龄" />
    				</el-form-item>
    				<el-form-item label="患者身份证" prop="code">
    					<el-input v-model="form.code" placeholder="请输入患者身份证" />
    				</el-form-item>
    				<el-form-item label="患者性别
    " prop="sex">
    					<el-select v-model="form.sex" placeholder="请选择患者性别
    ">
    						<el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
    							:value="dict.value"></el-option>
    					</el-select>
    				</el-form-item>
    				<el-divider content-position="center">门诊卡信息信息</el-divider>
    				<el-row :gutter="10" class="mb8">
    					<el-col :span="1.5">
    						<el-button type="primary" icon="el-icon-plus" size="mini"
    							@click="handleAddMedicalCostOutpatientInfo">添加</el-button>
    					</el-col>
    					<el-col :span="1.5">
    						<el-button type="danger" icon="el-icon-delete" size="mini"
    							@click="handleDeleteMedicalCostOutpatientInfo">删除</el-button>
    					</el-col>
    				</el-row>
    				<el-table :data="medicalCostOutpatientInfoList" :row-class-name="rowMedicalCostOutpatientInfoIndex"
    					@selection-change="handleMedicalCostOutpatientInfoSelectionChange" ref="medicalCostOutpatientInfo">
    					<el-table-column type="selection" width="50" align="center" />
    					<el-table-column label="序号" align="center" prop="index" width="50" />
    					<el-table-column label="金额" prop="money">
    						<template slot-scope="scope">
    							<el-input v-model="scope.row.money" placeholder="请输入金额" />
    						</template>
    					</el-table-column>
    					
    					<el-table-column label="状态 1启用 2挂失 3作废 4退卡" prop="status">
    						<template slot-scope="scope">
    							<el-input v-model="scope.row.status" placeholder="请输入状态 1启用 2挂失 3作废 4退卡" />
    						</template>
    					</el-table-column>
    					<el-table-column label="押金" prop="deposit">
    						<template slot-scope="scope">
    							<el-input v-model="scope.row.deposit" placeholder="请输入押金" />
    						</template>
    					</el-table-column>
    				</el-table>
    			</el-form>
    			<div slot="footer" class="dialog-footer">
    				<el-button type="primary" @click="submitForm">确 定</el-button>
    				<el-button @click="cancel">取 消</el-button>
    			</div>
    		</el-dialog>
    <!-- 门诊卡-->
    <el-dialog :title="title" :visible.sync="opena" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="个人档案id" prop="personid">
          <el-input v-model="form.personid" placeholder="请输入个人档案id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForma">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listArchives,
    getArchives,
    delArchives,
    addArchives,
    updateArchives
  } from "@/api/outpatient/archives";

  import {
    listInfo,
    getInfo,
    delInfo,
    addInfo,
    updateInfo
  } from "@/api/outpatient/info";

  export default {
    name: "Archives",
    dicts: ['sys_user_sex'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 子表选中数据
        checkedMedicalCostOutpatientInfo: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 个人档案表格数据
        archivesList: [],
        // 门诊卡信息表格数据
        medicalCostOutpatientInfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        opena:false,
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          age: null,
          code: null,
          sex: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询个人档案列表 */
      getList() {
        this.loading = true;
        listArchives(this.queryParams).then(response => {
          this.archivesList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          name: null,
          age: null,
          code: null,
          sex: null,
          personid:null
        };
        this.medicalCostOutpatientInfoList = [];
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.zengjia = 'b';
        this.reset();
        this.open = true;
        this.title = "添加个人档案";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getArchives(id).then(response => {
          this.form = response.data;
          this.medicalCostOutpatientInfoList = response.data.medicalCostOutpatientInfoList;
          this.open = true;
          this.title = "修改个人档案";
        });
      },
      /* 办卡*/
      banka(row) {
        this.reset();
        const id = row.id || this.ids
        getArchives(id).then(response => {
          this.form = response.data;
          this.medicalCostOutpatientInfoList = response.data.medicalCostOutpatientInfoList;
          this.form.personid=this.form.id
          this.opena = true;
          this.title = "办卡";
        });
      },

      submitForma() {
        this.$refs["form"].validate(valid => {
          if(valid){

          addInfo(this.form).then(response => {
            this.$modal.msgSuccess("办卡成功");
            this.opena = false;
            this.getList();
          });
          }
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.medicalCostOutpatientInfoList = this.medicalCostOutpatientInfoList;
            if (this.form.id != null) {
              updateArchives(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addArchives(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除个人档案编号为"' + ids + '"的数据项？').then(function() {
          return delArchives(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 门诊卡信息序号 */
      rowMedicalCostOutpatientInfoIndex({
        row,
        rowIndex
      }) {
        row.index = rowIndex + 1;
      },
      /** 门诊卡信息添加按钮操作 */
      handleAddMedicalCostOutpatientInfo() {
        let obj = {};
        obj.money = "";
        obj.enableTime = "";
        obj.lossTime = "";
        obj.deadTime = "";
        obj.returnTime = "";
        obj.status = "";
        obj.deposit = "";
        this.medicalCostOutpatientInfoList.push(obj);
      },
      /** 门诊卡信息删除按钮操作 */
      handleDeleteMedicalCostOutpatientInfo() {
        if (this.checkedMedicalCostOutpatientInfo.length == 0) {
          this.$modal.msgError("请先选择要删除的门诊卡信息数据");
        } else {
          const medicalCostOutpatientInfoList = this.medicalCostOutpatientInfoList;
          const checkedMedicalCostOutpatientInfo = this.checkedMedicalCostOutpatientInfo;
          this.medicalCostOutpatientInfoList = medicalCostOutpatientInfoList.filter(function(item) {
            return checkedMedicalCostOutpatientInfo.indexOf(item.index) == -1
          });
        }
      },
      /** 复选框选中数据 */
      handleMedicalCostOutpatientInfoSelectionChange(selection) {
        this.checkedMedicalCostOutpatientInfo = selection.map(item => item.index)
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('outpatient/archives/export', {
          ...this.queryParams
        }, `archives_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
